package puc.tcc.recomendaeventos;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BancoUser extends SQLiteOpenHelper{
	
	public BancoUser(Context context) {
        super(context, "user.sqlite", null, 1);
    }
 
    // Cria a tabela
    @Override
    public void onCreate(SQLiteDatabase db) {
    	db.execSQL("CREATE TABLE user(" +
                "_id integer primary key," +
                "nome VARCHAR(100)," +
                "email VARCHAR(100)," +
                "senha VARCHAR(100)," +
                "endereco VARCHAR(100),"+
                "interesses text," +
                "foto BLOB);");
    }
 
    // Atualizar a base de dados
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop na antiga tabela
        db.execSQL("DROP TABLE IF EXISTS user");
 
        // Create tables again
        onCreate(db);
    }
	
	// Acrescentar
	public void adicionar(ContentValues prod) {		
	    SQLiteDatabase db = this.getWritableDatabase();
	 
	    db.insert("user", null, prod);
	}
	
	// Buscar os jogos
	public Cursor buscar() {
		SQLiteDatabase db = this.getReadableDatabase();
	    
		Cursor cursor = db.query("user", new String[] {"_id", "nome", "email", "senha", "endereco","interesses", "foto"}, null, null, null, null, "nome", null);
	    if (cursor != null)
	    	 return cursor;
	    else{
	    	return null;
	    }
	}
	
	// Buscar user pelo id
	public Cursor buscar(String id) {
		SQLiteDatabase db = this.getReadableDatabase();
	    
		Cursor cursor = db.query("user", new String[] {"_id", "nome", "email", "senha", "endereco", "interesses", "foto"}, "_id = ?",
	            new String[] {id}, null, null, null, null);
	    if (cursor != null)
	        cursor.moveToFirst();
	    else{
	    	return null;
	    }

	    return cursor;
	}
	
	//excluir
	public void excluir(String id){
		SQLiteDatabase db = this.getWritableDatabase();
		
		db.delete("user", "_id = " + id, null);
	}
	
	//atualizar
	public void atualizar(String id, ContentValues user){		
	    SQLiteDatabase db = this.getWritableDatabase();
	 
	    // updating row
	    db.update("user", user, "_id = ?", new String[] {id});
	}
}
